<?php

namespace app\api\controller\meet\article;

use app\common\controller\Api;


class Article extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     *
     * @var \app\common\model\meet\article\Article
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();

        $this->model = new \app\common\model\meet\article\Article();
    }

    public function index()
    {
        $category_id = $this->request->get('category_id/d', 0);
        $page = $this->request->get('page/d', 1);
        $page_size = $this->request->get('page_size/d', 10);

        $condition = [
            'status' => $this->model::STATUS_NORMAL
        ];

        if ($category_id) {
            $condition['category_id'] = $category_id;
        }

        $list = $this->model
            ->where($condition)
            ->field('id,image,title,createtime')
            ->order(['weigh' => 'desc'])
            ->paginate(['list_rows' => $page_size, 'page' => $page]);

        $this->success('', $list);
    }

    public function detail()
    {
        $id = $this->request->get('id/d', 0);

        if (empty($id)) {
            $this->error(__('Parameter %s can not be empty', ''));
        }

        $condition = [
            'id' => $id,
            'status' => $this->model::STATUS_NORMAL
        ];

        $detail = $this->model->where($condition)->find();

        if (empty($detail)) {
            $this->error(__('The product does not exist'));
        }

        $this->success('', $detail);
    }
}
